import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/* Router Modules */

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/auth-redirect",
    component: () => import("@/views/login/auth-redirect"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401"),
    hidden: true,
  },
  // 导航栏路由
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index"),
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        // component: () => import("@/views/dashboard/index"),
        component: () => import("@/views/dashboard/admin/index"),
        name: "Dashboard",
        meta: { title: "首页", icon: "dashboard", affix: false },
      },
    ],
  },
  {
    path: "/template/particulars",
    component: Layout,
    children: [
      {
        path: "particulars",
        component: () => import("@/views/template/particulars/index"),
        name: "particulars",
        hidden: true,
        meta: { title: "模板内容", icon: "user", affix: false },
      },
    ],
  },
  {
    path: "/architectural",
    component: Layout,
    children: [
      {
        path: "architectural",
        component: () => import("@/views/Architectural/index"),
        name: "architectural",
        meta: { title: "建筑类目", icon: "user", noCache: true },
      },
    ],
    hidden: true,
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/xiangmu",
    component: Layout,
    // alwaysShow: true, // will always show the root menu
    meta: { title: "项目列表", icon: "skill", roles: ["three"] },
    children: [
      {
        path: "xiangmu",
        component: () => import("@/views/xiangmu"),
        name: "xm",
        meta: {
          title: "项目列表",
          icon: "skill",
          affix: false,
          roles: ["three"],
        },
      },
      {
        path: "xq",
        component: () => import("@/views/xq"),
        name: "xq",
        hidden: true,
        meta: {
          title: "造价录入",
          icon: "dashboard",
          affix: false,
          roles: ["three", "one", "two"],
        },
      },
    ],
  },
  {
    path: "/template",
    component: Layout,
    children: [
      {
        path: "template",
        component: () => import("@/views/template/index"),
        name: "template",
        meta: {
          title: "模板管理",
          icon: "component",
          affix: false,
          roles: ["two"],
        },
      },
    ],
  },
  {
    path: "/project",
    component: Layout,
    children: [
      {
        path: "project",
        component: () => import("@/views/project/index"),
        name: "project",
        meta: {
          title: "项目管理",
          icon: "list",
          affix: false,
          roles: ["one", "two"],
        },
      },
    ],
  },
  {
    path: "/store",
    component: Layout,
    children: [
      {
        path: "store",
        component: () => import("@/views/project/store"),
        name: "store",
        hidden: true,
        meta: {
          title: "仓库管理",
          icon: "list",
          affix: false,
          roles: ["one", "two"],
        },
      },
    ],
  },

  {
    path: "/register",
    component: Layout,
    children: [
      {
        path: "register",
        component: () => import("@/views/project/register"),
        name: "register",
        hidden: true,
        meta: {
          title: "仓库历史记录",
          icon: "list",
          affix: false,
          roles: ["one", "two"],
        },
      },
    ],
  },
  {
    path: "/role",
    component: Layout,
    children: [
      {
        path: "role",
        component: () => import("@/views/role/role"),
        name: "role",
        meta: {
          title: "角色列表",
          icon: "people",
          affix: false,
          roles: ["two"],
        },
      },
    ],
  },
  {
    path: "/user",
    component: Layout,
    children: [
      {
        path: "user",
        component: () => import("@/views/user/userlist"),
        name: "userlist",
        meta: {
          title: "用户列表",
          icon: "user",
          affix: false,
          roles: ["two"],
        },
      },
    ],
  },
  {
    path: "store",
    component: () => import("@/views/store/index"),
    name: "store",
    meta: {
      title: "仓库",
      icon: "user",
      affix: false,
      roles: ["one", "two"],
    },
    hidden: true,
  },
  {
    path: "/storelist",
    component: Layout,
    children: [
      {
        path: "/storelist",
        component: () => import("@/views/store/storeList"),
        name: "storelist",
        meta: {
          title: "材料录入",
          icon: "lock",
          affix: false,
          roles: ["one", "two", "three"],
        },
        hidden: true,
      },
    ],
  },

  {
    path: "/Statistics",
    component: Layout,
    children: [
      {
        path: "/Statistics",
        component: () => import("@/views/Statistics/index"),
        name: "Statistics",
        hidden: true,
        meta: {
          title: "统计",
          icon: "list",
          affix: false,
          roles: ["two"],
        },
      },
    ],
  },

  // {
  //   path: "/Statistics",
  //   component: () => import("@/views/Statistics/index"),
  //   name: "Statistics",
  //   meta: {
  //     title: "统计",
  //     icon: "Statistics",
  //     affix: false,
  //     roles: ["one", "two"],
  //   },
  //   hidden: true,
  // },

  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
